import request from '../../utils/request';
import PublicService from '../../services/PublicService'
import StationRuleService from '../../services/stationRule/stationRuleService'
import {message} from 'antd';

export default {
  namespace: 'stationRule',
  state: {

    isAreaItemModal: true,// true为 区域管理
    // 区域管理
    selectedRowsArea: [], // 选中行
    areaName: '',
    size: 10,
    page: 1,
    total: 20,
    areaNameDown: [],
    isVisibleArea: false,
    dataSourceArea: [],
    initDataArea: {},
    initDataFactor: {},


    // 设备管理
    selectedRowsDevice: [],
    deviceName: '',
    sizeDevice: 10,
    pageDevice: 1,
    totalDevice: 20,
    deviceNameDown: [],
    deviceTypeNameDown: [],
    deviceAreaNameDown: [],
    isVisibleDevice: false,
    dataSourceDevice: [],
    initDataDevice: {},


    // 设置公共变量
    loading: false,
    dataSource: [],
    isEdit: false, // false 为可编辑
  },
  effects: {

    // 获取区域下拉数据
    * getAreaNameDown({payload}, {put, call, select}) {
      let {ret} = yield call(StationRuleService.getAreaNameDown);
      try {
        let areaNameDown = ret.map(item => {
          return {value: item, text: item}
        });
        yield put({type: 'saveInfo', payload: {areaNameDown}})
      } catch (e) {
        console.log(e)
      }
    },
    // 获取页面表格数据
    * getPageListArea({payload}, {put, call, select}) {
      yield put({type: 'saveInfo', payload: {loading: true}});
      let areaName = yield select(state => state.stationRule.areaName);
      let size = yield select(state => state.stationRule.size);
      let page = yield select(state => state.stationRule.page);
      let params = {areaName, size, page};
      let {ret} = yield call(StationRuleService.getPageListArea, params);
      console.log(ret);
      try {
        yield put({type: 'save', payload: {dataSourceArea: ret.rows, total: ret.total, loading: false}})
      } catch (e) {
        console.log(e)
      }

    },
    // 编辑和查看
    * editReadHandle({payload}, {call, put, select}) {
      let {ret} = yield call(StationRuleService.getAreaInfo,{areaId:payload.areaId});
      console.log(ret);
      delete payload.areaId;
      yield put({type: 'saveInfo', payload:{...payload,initDataArea:ret}});
    },
    // 删除函数
    * deleteHandle({payload}, {put, call, select}) {
      let selectRow = yield select(state => state.stationRule.selectedRowsArea);
      if (selectRow.length === 0) {
        message.warn('未选择');
        return
      }
      let ids = selectRow.join(',');
      console.log(ids);
      let res = yield call(StationRuleService.deleteArea, {areaIds:ids});
      if(res.rc === 0){
        message.success(res.ret)
      }else{
        message.error(res.err)
      }
      yield put({type:'saveInfo',payload:{selectedRowsArea:[]}});
      yield put({type: 'getPageListArea'})
    },
    // page改变时候
    * pageChangeHandle({payload}, {put, call, select}) {
      yield put({type: 'saveInfo', payload});
      yield put({type: 'getPageListArea'})
    },

    // 区域空间
    *search({payload},{put,call}){
      yield put({type:'saveInfo',payload:payload});
      yield put({type:'getPageListArea'});
    },

    // 保存新增区域
    * submit({payload}, {put, call, select,}) {
      let initDataArea = yield select(state => state.stationRule.initDataArea);
      let res;
      if(initDataArea.id){
        res = yield call(StationRuleService.updateArea, {...initDataArea,...payload.params});
      }else{
        res = yield call(StationRuleService.saveArea, payload.params);
      }
      console.log(res);
      if (res.rc === 1) {
        message.error(res.err);
      } else {
        message.success(res.ret)
      }
      yield put({type: 'saveInfo', payload: {isVisibleArea: false, page: 1, size: 10, initDataArea: {}}});
      yield put({type: 'getPageListArea'})
    },
    * submitFactor({payload}, {put, call, select}) {
      let initDataFactor = yield select(state => state.stationRule.initDataFactor);
      let data = {...initDataFactor, ...payload.params};
      console.log(data);
      let ret = yield call(StationRuleService.saveFactorInfo, data);
      console.log(ret);
      if (ret.rc === 0) {
        message.success(ret.ret)
      } else {
        message.error(ret.err)
      }
      yield put({type: 'saveInfo', payload: {isVisibleArea: false}})
    },
    // 厂信息
    * readFactorHandle({payload}, {call, put, select}) {
      let {ret} = yield call(StationRuleService.getFactoryInfo);
      console.log(ret);
      yield put({type: 'saveInfo', payload: {...payload, initDataFactor: ret}});

    },


    // 设备信息

    // 获取设备下拉数据
    * getDeviceNameDown({payload}, {put, call, select}) {
      let {ret} = yield call(StationRuleService.getDeviceNameDownDevice);
      console.log(ret);
      try {
        let deviceNameDown = ret.map(item => {
          return {value: item, text: item}
        });
        yield put({type: 'saveInfo', payload: {deviceNameDown}})
      } catch (e) {
        console.log(e)
      }
    },

    // 获取设备区域下拉
    * getDeviceAreaNameDown({payload}, {put, call, select}) {
      let {ret} = yield call(StationRuleService.getDeviceAreaNameDownDevice);
      try {
        let deviceAreaNameDown = ret.map(item => {
          return {value: item.id, text: item.areaName}
        });
        yield put({type: 'saveInfo', payload: {deviceAreaNameDown}})
      } catch (e) {
        console.log(e)
      }
    },
    // 获取设备类型下拉
    * getDeviceTypeNameDown({payload}, {put, call, select}) {
      let {ret} = yield call(StationRuleService.getDeviceTypeNameDownDevice);
      console.log(ret);
      try {
        let deviceTypeNameDown = [];
        for (let i in ret) {
          deviceTypeNameDown.push({
            value: ret[i],
            text: ret[i]
          })
        }
        yield put({type: 'saveInfo', payload: {deviceTypeNameDown}})
      } catch (e) {
        console.log(e)
      }
    },
    // page改变时候
    * pageChangeDeviceHandle({payload}, {put, call, select}) {
      yield put({type: 'saveInfo', payload});
      yield put({type: 'getPageListDevice'})
    },
    // 获取页面表格数据
    * getPageListDevice({payload}, {put, call, select}) {
      yield put({type: 'saveInfo', payload: {loading: true}});
      let deviceName = yield select(state => state.stationRule.deviceName);
      let size = yield select(state => state.stationRule.sizeDevice);
      let page = yield select(state => state.stationRule.pageDevice);
      let params = {deviceName, size, page};
      console.log(params);
      let {ret} = yield call(StationRuleService.getPageListDevice, params);
      console.log(ret);
      try {
        yield put({type: 'save', payload: {dataSourceDevice: ret.rows, totalDevice: ret.total, loading: false}})
      } catch (e) {
        console.log(e)
      }

    },
    // 点击查看数据
    *editReadDeviceHandle({payload},{put,call,select}){
      let {ret} = yield call(StationRuleService.getDeviceInfoDevice,{deviceId:payload.deviceId});
      console.log(ret);
      delete payload.deviceId;
      yield put({type:'saveInfo',payload:{initDataDevice:ret,...payload}});
    },
    * deleteDeviceHandle({payload}, {put, call, select}) {
      let selectRow = yield select(state => state.stationRule.selectedRowsDevice);
      if (selectRow.length === 0) {
        message.warn('未选择');
        return
      }
      let ids = selectRow.join(',');
      console.log(ids);
      let res = yield call(StationRuleService.deleteDevice, {deviceIds:ids});
      if(res.rc === 0){
        message.success(res.ret)
      }else{
        message.error(res.err)
      }
      yield put({type:'saveInfo',payload:{selectedRowsDevice:[]}});
      console.log(ret);
      yield put({type: 'getPageListArea'})
    },
    // 保存新增区域
    * submitDevice({payload}, {put, call, select,}) {
      let initDataDevice = yield select(state => state.stationRule.initDataDevice);
      let res,data;
      if(initDataDevice.id){
        data = {...initDataDevice,...payload.params};
        res = yield call(StationRuleService.updateDevice, data);
      }else{
        res = yield call(StationRuleService.saveDevice, payload.params);
      }
      console.log(res);
      if (res.rc === 1) {
        message.error(res.err);
      } else {
        message.success(res.ret)
      }
      yield put({
        type: 'saveInfo',
        payload: {isVisibleDevice: false, pageDevice: 1, sizeDevice: 10, initDataDevice: {}}
      });
      yield put({type: 'getPageListDevice'})
    },

    *searchDevice({payload},{put,call}){
      yield put({type:'saveInfo',payload:payload.params});
      yield put({type:'getPageListDevice'});
    },

    * saveInfo({payload}, {put, call, take, select}) {
      let obj = PublicService.deepClone(payload);
      yield put({type: 'save', payload: {...obj}});
    }
  },
  reducers: {
    save(state, {payload}) {
      console.log(payload, 'reduce');
      return {...state, ...payload};
    },
  },
};
